import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';

import { CookieService } from 'ng2-cookies';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/delay';

@Injectable()
export class AuthService {
  isLoggedIn = false;

  redirectUrl: string;

  constructor(
    private http: Http,
    private cookieService: CookieService
  ) { }

  login(accessToken: string): void {
    sessionStorage.setItem('access_token', accessToken);
    this.isLoggedIn = true;
  }

  getUserByAccessToken(accessToken: string): Observable<any> {
    let url = "https://gitee.com/api/v5/user";
    let params = {
      access_token: accessToken
    }
    return this.http
      .get(url, {
        params: params
      })
      .map(user => user.json());
  }

  logout(): void {
    this.isLoggedIn = false;
  }

}
